package com.symone.webcms.controller.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.Form;
import com.symone.webcms.model.FormField;
import com.symone.webcms.model.Slide;
import com.symone.webcms.model.SlidePicture;
import com.symone.webcms.model.vo.SlidePictureVO;
import com.symone.webcms.service.FormFieldService;
import com.symone.webcms.service.FormService;
import com.symone.webcms.service.SlidePictureService;
import com.symone.webcms.service.SlideService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/cms/extend")
public class ExtendController extends BaseController {

    @Autowired
    private SlideService slideService;
    @Autowired
    private SlidePictureService slidePictureService;
    @Autowired
    private FormService formService;
    @Autowired
    private FormFieldService formFieldService;

    @RequiresPermissions("extend:slide:index")
    @RequestMapping(value = "/slide/index", method = RequestMethod.GET)
    public ModelAndView slideList() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/extend/slide/index");
        return mv;
    }

    @RequiresPermissions("extend:slide:add")
    @RequestMapping(value = "/slide/add", method = RequestMethod.GET)
    public ModelAndView slideAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/extend/slide/form");
        mv.addObject("slide",new Slide());
        return mv;
    }

    @RequiresPermissions("extend:slide:edit")
    @RequestMapping(value = "/slide/edit", method = RequestMethod.GET)
    public ModelAndView slideEdit(Long id) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/extend/slide/form");
        Slide slide = slideService.selectById(id);
        mv.addObject("slide",slide);
        return mv;
    }

    @RequiresPermissions("extend:slide:picture:index")
    @RequestMapping(value = "/slide/picture/index", method = RequestMethod.GET)
    public ModelAndView slidePictureList(Long slideId) {
        ModelAndView mv = new ModelAndView();
        List<Slide> slides = slideService.selectList(
                new EntityWrapper<Slide>()
                .eq("deleted",0)
                .and()
                .eq("lang",getSessionLanguage().getCode())
        );
        Slide slide = null;
        for (Slide s : slides) {
            if(slideId == null || slideId == 0) {
                slide = s;
                break;
            }else if(s.getId().equals(slideId)) {
                slide = s;
                break;
            }
        }
        List<SlidePicture> pictures = slidePictureService.selectList(
                new EntityWrapper<SlidePicture>()
                .eq("deleted",0)
                .and()
                .eq("slide_id",slide.getId())
                .and()
                .eq("lang",getSessionLanguage().getCode())
                .orderBy("sorting",true)
        );
        List<SlidePictureVO> resultList = new ArrayList<SlidePictureVO>();
        for (SlidePicture sp : pictures){
            resultList.add( new SlidePictureVO(sp));
        }

        mv.addObject("slide",slide);
        mv.setViewName("/extend/slide/picture/index");
        mv.addObject("pictures",resultList);
        mv.addObject("slides",slides);
        return mv;
    }

    @RequiresPermissions("extend:slide:picture:add")
    @RequestMapping(value = "/slide/picture/add", method = RequestMethod.GET)
    public ModelAndView slidePictureAdd(Long slideId) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/extend/slide/picture/form");
        mv.addObject("slide",slideService.selectById(slideId));
        mv.addObject("picture",new SlidePicture());
        return mv;
    }

    @RequiresPermissions("extend:slide:picture:edit")
    @RequestMapping(value = "/slide/picture/edit", method = RequestMethod.GET)
    public ModelAndView slidePictureEdit(Long id) {
        SlidePicture picture = slidePictureService.selectById(id);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/extend/slide/picture/form");
        mv.addObject("slide",slideService.selectById(picture.getSlideId()));
        mv.addObject("picture",picture);
        return mv;
    }

    @RequiresPermissions("extend:form:index")
    @RequestMapping(value = "/form/index", method = RequestMethod.GET)
    public ModelAndView formList() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/extend/form/index");
        return mv;
    }

    @RequiresPermissions("extend:form:add")
    @RequestMapping(value = "/form/add", method = RequestMethod.GET)
    public ModelAndView formAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/extend/form/form");
        mv.addObject("form",new Form());
        return mv;
    }

    @RequiresPermissions("extend:form:edit")
    @RequestMapping(value = "/form/edit", method = RequestMethod.GET)
    public ModelAndView formEdit(Long id) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/extend/form/form");
        mv.addObject("form",formService.selectById(id));
        return mv;
    }

    @RequiresPermissions("extend:form:index")
    @RequestMapping(value = "/form/field/index", method = RequestMethod.GET)
    public ModelAndView fieldList(Long formId) {
        ModelAndView mv = new ModelAndView();
        List<Form> forms = formService.findAll();
        Form form = null;
        for (Form f : forms) {
            if(formId == null || formId == 0) {
                form = f;
                break;
            }else if(f.getId().equals(formId)) {
                form = f;
                break;
            }
        }
        mv.setViewName("/extend/form/field/index");
        mv.addObject("forms",forms);
        mv.addObject("form",form);
        return mv;
    }

    @RequiresPermissions("extend:form:add")
    @RequestMapping(value = "/form/field/add", method = RequestMethod.GET)
    public ModelAndView fieldAdd(Long formId) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/extend/form/field/form");
        mv.addObject("form",formService.selectById(formId));
        mv.addObject("field",new FormField());
        mv.addObject("lang",getSessionLanguage().getCode());
        return mv;
    }

    @RequiresPermissions("extend:form:edit")
    @RequestMapping(value = "/form/field/edit", method = RequestMethod.GET)
    public ModelAndView fieldEdit(Long id) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/extend/form/field/form");
        mv.addObject("field",formFieldService.selectById(id));
        mv.addObject("lang",getSessionLanguage().getCode());
        return mv;
    }

}
